package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.dto.ReaderLendDto;
import com.zysf.library_boot.entity.BookInfo;
import com.zysf.library_boot.entity.NavMenu;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.entity.TypeInfo;
import com.zysf.library_boot.service.ReaderInfoService;
import com.zysf.library_boot.util.Result;
import com.zysf.library_boot.util.UploadFile;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reader")
public class ReaderInfoController {
    @Autowired
    ReaderInfoService readerInfoService;

    //排行榜
    @RequestMapping("/leaderboard")
    public Result leaderboard() {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.orderByDesc("numberborrow");
        qw.last("limit 10");
        List<ReaderInfo> readers = readerInfoService.list(qw);
        return Result.ok(readerInfoService.list(qw));
    }
    //优秀读者
    @RequestMapping("/excellentReaders")
    public Result excellentReaders() {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.ge("numberborrow", 6);
        qw.orderByDesc("numberborrow");
        qw.last("limit 24");
        List<ReaderInfo> readers = readerInfoService.list(qw);
        return Result.ok(readerInfoService.list(qw));
    }
// 删除
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean a = readerInfoService.removeById(id);
        return Result.ok(a);
    }
// 批量删除
    @RequestMapping("/batchDeleteReader")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        boolean b = readerInfoService.removeByIds(ids);
        return Result.ok(b);
    }
// 分页查询
    @RequestMapping("/getReader")
    public Result getReader(@RequestParam(required = false) String readernumber,
                            @RequestParam(required = false) String username,
                            @RequestParam(required = false) String tel,
                            @RequestParam int currentPage,
                            @RequestParam int pageSize) {
        QueryWrapper<ReaderInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(readernumber)) {
            queryWrapper.like("readernumber", readernumber);
        }
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.like("username", username);
        }
        if (StringUtils.isNotBlank(tel)) {
            queryWrapper.like("tel", tel);
        }

        Page<ReaderInfo> page = new Page<>(currentPage, pageSize);
        IPage<ReaderInfo> readerInfoPage = readerInfoService.page(page, queryWrapper);

        Map<String, Object> result = new HashMap<>();
        result.put("records", readerInfoPage.getRecords());
        result.put("total", readerInfoPage.getTotal());

        return Result.ok(result);
    }
// 添加读者信息
    @PostMapping("/addReader")
    public Result addReader(@RequestBody ReaderInfo readerInfo) {
        boolean saved = readerInfoService.save(readerInfo);
        if (saved) {
            return Result.ok("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }
// 检查读者号是否重复
@RequestMapping("/checkReaderNumber")
public Result checkReaderNumber(@RequestParam String readernumber, @RequestParam(required = false) Long id) {
    QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
    qw.eq("readernumber", readernumber);
    if (id != null) {
        qw.ne("id", id); // 排除当前编辑的读者
    }
    ReaderInfo readerInfoRes = readerInfoService.getOne(qw);
    return Result.ok(readerInfoRes);
}
// 检查账号是否重复
    @RequestMapping("/checkUsername")
    public Result checkUsername(@RequestParam String username, @RequestParam(required = false) Long id) {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.eq("username", username);
        if (id != null) {
            qw.ne("id", id); // 排除当前编辑的读者
        }
        ReaderInfo readerInfoRes = readerInfoService.getOne(qw);
        return Result.ok(readerInfoRes);
    }

    @RequestMapping("/checkRealname")
    public Result checkRealname(@RequestParam String realname, @RequestParam(required = false) Long id) {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.eq("realname", realname);
        if (id != null) {
            qw.ne("id", id); // 排除当前编辑的读者
        }
        ReaderInfo readerInfoRes = readerInfoService.getOne(qw);
        return Result.ok(readerInfoRes);
    }

// 修改读者信息
    @PutMapping("/updateReader")
    public Result updateReader(@RequestBody ReaderInfo readerInfo) {
        boolean updated = readerInfoService.updateById(readerInfo);
        if (updated) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @RequestMapping("/getReaderById")
    public Result getReaderById(@RequestParam Long id){
        ReaderInfo readerInfo = readerInfoService.getById(id);
        if (readerInfo != null) {
            return Result.ok(readerInfo);
        } else {
            return Result.fail("读者不存在");
        }
    }

    @RequestMapping("/getReaderNumbers")
    public Result getReaderNumbers(){
        List<ReaderInfo> readerInfoList = readerInfoService.list();
        return Result.ok(readerInfoList);
    }

    @RequestMapping("/update")
    public Result update(@RequestParam(name = "file", required = false) MultipartFile file, ReaderInfo readerInfo) {
        if (file!= null){
            String imgUrl = UploadFile.fileUrl(file);
            readerInfo.setIcon(imgUrl);
        }
        boolean k = readerInfoService.updateById(readerInfo);
        if (k){
            return Result.ok(readerInfo);
        }
        return Result.fail("修改失败");
    }

    @RequestMapping("/getUserById")
    public Result getUserById(Integer id){
        ReaderInfo readerInfo  =  readerInfoService.getById(id);
        return Result.ok(readerInfo);
    }

    @RequestMapping("/getReaderLendList")
    public Result getReaderLendList(@RequestParam Integer id, @RequestParam int currentPage, @RequestParam int pageSize) {
        Page<ReaderLendDto> page = new Page<>(currentPage, pageSize);
        IPage<ReaderLendDto> readerLendDtoPage  = readerInfoService.getReaderCustomLendList(id, page);

        Map<String, Object> result = new HashMap<>();
        result.put("records", readerLendDtoPage.getRecords());
        result.put("total", readerLendDtoPage.getTotal());

        return Result.ok(result);
    }

    @RequestMapping("/validateOldPassword")
    public Result validateOldPassword(@RequestParam Long id, @RequestParam String password) {
        ReaderInfo readerInfo = readerInfoService.getById(id);
        if (readerInfo != null && readerInfo.getPassword().equals(password)) {
            return Result.ok("旧密码正确");
        } else {
            return Result.fail("旧密码错误");
        }
    }

    @RequestMapping("/validateReaderWriteInfo")
    public Result validateReaderWriteInfo(@RequestParam Long id, @RequestParam String password,@RequestParam String readernumber,@RequestParam String tel) {
        ReaderInfo readerInfo = readerInfoService.getById(id);
        if (readerInfo != null && readerInfo.getPassword().equals(password) && readerInfo.getReadernumber().equals(readernumber) && readerInfo.getTel().equals(tel)) {
            return Result.ok("输入信息正确");
        } else {
            return Result.fail("输入信息错误");
        }
    }

    @RequestMapping("/ReaderCount")
    public Result ReaderCount(){
        QueryWrapper<ReaderInfo> qwReader = new QueryWrapper<>();
        qwReader.eq("role",0);
        int readercount = (int) readerInfoService.count(qwReader);
        Map<String,Object> map = new HashMap<>();
        map.put("readercount",readercount);
        return Result.ok(map);
    }

    @RequestMapping("/SexCount")
    public Result SexCount(){
        //男
        QueryWrapper<ReaderInfo> qwSexman = new QueryWrapper<>();
        qwSexman.eq("sex","男");
        int man = (int) readerInfoService.count(qwSexman);
        //女
        QueryWrapper<ReaderInfo> qwSexwoman =new QueryWrapper<>();
        qwSexwoman.eq("sex","女");
        int woman = (int) readerInfoService.count(qwSexwoman);
        //输出总数
        Map<String,Object> Sex = new HashMap<>();
        Sex.put("man",man);
        Sex.put("woman",woman);
        return Result.ok(Sex);
    }

    @RequestMapping("/MemberCount")
    public Result MemberCount(){
        QueryWrapper<ReaderInfo> qw =new QueryWrapper<>();
        qw.select("address","count(*) as count");
        qw.groupBy("address");
        List<Map<String,Object>> maps = readerInfoService.listMaps(qw);
        return Result.ok(maps);
    }
}
